//var $j = jQuery.noConflict();
/* uncomment this block if there are some conflicts with other js library/framework */
(function ($) {

MyPage = {
    element: {
        btnSelectAllServices: null
		, btnReset: null
		, frmSearch: null
        , btnSelectAllEstates: null
    },

    builder: {},

    application: {
		resetForm: function ( ){
			MyPage.element.frmSearch.find('input:checkbox[name="chkServiceType[]"]').attr('checked' , false ).attr('selected' , false );
			MyPage.element.frmSearch.find('input:checkbox[name="chkRegion[]"]').attr('checked' , false ).attr('selected' , false );
			//MyPage.element.frmSearch.each (function() {this.reset();});
			MyPage.application.populateRegion_in_lstEstate();
		},
		
        populateRegion_in_lstEstate: function ( SelectAll ) {
			//$("#frmSearch").find('select[name="lstEstate"] optgroup option' ).attr( 'selected' , false );
			$("#frmSearch").find("input[class='chkRegion']").each( function(index , eachElement) { 
				var title = eachElement.title;
				var checked = eachElement.checked;
				if ( checked )
				{
					$("#frmSearch").find('select[name="lstEstate[]"] optgroup[label="' + title + '"]' ).show();
					
					if ( typeof SelectAll !== 'undefined')
					{
						$("#frmSearch").find('select[name="lstEstate[]"] optgroup[label="' + title + '"] option' ).attr( 'selected' , SelectAll );
					}
				}
				else
				{
					$("#frmSearch").find('select[name="lstEstate[]"] optgroup[label="' + title + '"] option' ).attr( 'selected' , false );
					$("#frmSearch").find('select[name="lstEstate[]"] optgroup[label="' + title + '"]' ).hide();
				}
			});
			//$("#frmSearch").find('select[name="lstEstate[]"] optgroup[location="' + index + '"]' ).show();
		}

    },

    prepareElement: function () {
        this.element = {
            btnSelectAllServices: $("#btnSelectAllServices")
			, btnReset: $("#btnReset")
			, frmSearch: $("#frmSearch")
			, btnSearchVoluntaryOrganisation: $("#btnSearchVoluntaryOrganisation")
			, btnSearchPrivateOrganisation: $("#btnSearchPrivateOrganisation")
			, btnSearchGovernmentOrganisation: $("#btnSearchGovernmentOrganisation")
			, btnSearchAllOrganisation: $("#btnSearchAllOrganisation")
            , btnSelectAllEstates: $("#btnSelectAllEstates")
        };
    },

    prepareView: function () {
        this.element.btnSelectAllServices.button();
		this.element.btnReset.button();
		$("#frmSearch").find("input[name='hidRegions[]']").each( function(index , eachElement) { 
			$("#frmSearch").find('select[name="lstEstate[]"] optgroup' ).hide();
		});
        this.element.btnSelectAllEstates.button();
    },

    eventBinding: function () {
		
        this.element.btnSelectAllServices.click(function () {
			MyPage.element.frmSearch.find('input:checkbox[name="chkServiceType[]"]').attr('checked' , true ).attr('selected' , true );
        });
        this.element.btnReset.click(function () {
			MyPage.application.resetForm();
        });
        this.element.frmSearch.submit(function () {
			var isChecked = MyPage.element.frmSearch.find('input[name="chkServiceType[]"]:checked');
			if ( ! isChecked.length)
			{
				App.alert("Please select at least one Service" , "" )
				return false;
			}
        });

		this.element.frmSearch.delegate('.btnOrganisation', 'click', function () {
			var id = $(this).attr("itemprop");
			$("#hidOrganisationType").val(id);
			MyPage.element.frmSearch.submit();
        });

        this.element.frmSearch.delegate('.chkRegion', 'click', function () {
			MyPage.application.populateRegion_in_lstEstate ( );
        });

        this.element.btnSelectAllEstates.click(function () {
			MyPage.application.populateRegion_in_lstEstate ( true );
        });
    },

    main: function () {
        this.prepareElement();
        this.prepareView();
        this.eventBinding();
    }
};

$(function () {
    MyPage.main();
});

/* uncomment this block if there are some conflicts with other js library/framework */
})(jQuery);

